const ModBusRtuMasterPlugin= require("../plugins/ModBusRtuMasterPlugin.js");
const Utils=require("../utils/Utils.js")
modBusRtuMasterPlugin=  new ModBusRtuMasterPlugin({com:"COM18",brand:115200})
//dda信息位置
PMD_ADDR_directory_dda     =       0
PMD_ADDR_directory_dda_ver  =      0
PMD_ADDR_directory_dda_entryNum =  1
PMD_ADDR_directory_dda_entry   =   2
class DdaEntry{
     classNode;
     isSingleton;
     instanceNum;
     firstId;
     eachSpace;
};
class DdaInfo
{
    ver;
    entryNum;
    entry=[];
};

async function main(){
    ddaInfo=new DdaInfo();
    let r=await  modBusRtuMasterPlugin.readHoldingRegisters(247,PMD_ADDR_directory_dda_entryNum,1)
    ddaInfo.entryNum=r[0];
     r=await  modBusRtuMasterPlugin.readHoldingRegisters(247,PMD_ADDR_directory_dda_ver,1)
    ddaInfo.ver=r[0];
    for (let i=0;i<ddaInfo.entryNum;i++){
        let entryItem=new DdaEntry();
        let ddaEntityBuf=await  modBusRtuMasterPlugin.readHoldingRegisters(247,PMD_ADDR_directory_dda_entry+ i * 12,12);
        let classNodeBuf=ddaEntityBuf.splice(0,8);
        let classNodeStrBuf=[];
        for (let j=0;j<classNodeBuf.length;j++){
            if(classNodeBuf[j]==0 || classNodeBuf[j]>>8==0){
                break;
            }
            classNodeStrBuf[2*j]=classNodeBuf[j]>>8;
            if(classNodeBuf[j]&0xff){
                classNodeStrBuf[2*j+1]=classNodeBuf[j]&0xff;
            }else {
                break;
            }

        }
        entryItem.classNode= String.fromCharCode.apply(String, classNodeStrBuf);
        entryItem.isSingleton= ddaEntityBuf.splice(0,1)[0];
        entryItem.instanceNum=ddaEntityBuf.splice(0,1)[0];
        entryItem.firstId=ddaEntityBuf.splice(0,1)[0];
        entryItem.eachSpace=ddaEntityBuf.splice(0,1)[0];
        entryItem.isSingleton=Utils.hexStringByWord(entryItem.isSingleton)
        entryItem.instanceNum=Utils.hexStringByWord(entryItem.instanceNum)
        entryItem.firstId=Utils.hexStringByWord(entryItem.firstId)
        entryItem.eachSpace=Utils.hexStringByWord(entryItem.eachSpace)
        ddaInfo.entry.push(entryItem);
        //console.log("0x"+ firstId.toString(16))

    }
    console.log(ddaInfo)

    process.exit()


}



main()
